package com.whz.workmanageol.utils;

import com.whz.workmanageol.dao.StudentMapper;
import com.whz.workmanageol.dao.WorkMapper;
import com.whz.workmanageol.entity.Student;
import com.whz.workmanageol.entity.Work;
import com.whz.workmanageol.vo.FilePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

/**
 * 文件上传工具类
 */
@Component
public class FileUpload {
    private static final Logger logger = LoggerFactory.getLogger(FileUpload.class);
    public static final String FILE_UPLOAD_PATH = "F:"+File.separator+"WorkManageOL";
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 学生提交作业 本地需要替换掉原来的文件
     * @param file
     * @param workid
     */
    public void replaceWork(MultipartFile file, String workid){
        Work work = workMapper.selectByPrimaryKey(workid);
//        Work work = workRepository.findById(workid).get();
        // 原作业路径
        String filePath = work.getFilepath();
        String filename = work.getFilename();
        String suffix = work.getSuffix();

        File dest = new File(FILE_UPLOAD_PATH + filePath + filename + suffix);
        // 检测是否存在目录
        if(!dest.getParentFile().exists()){
            // 新建文件夹
            dest.getParentFile().mkdirs();
        }
        try {
            if(dest.exists() && dest.isFile()){
                dest.delete();
            }
            // 文件写入
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 教师发布作业对应的本地文件操作
     * @param file
     * @param filePath
     */
    public void teacherPublish(MultipartFile file, FilePath filePath){
        // 1.上传附件到本地
        uploadAttachment(file, filePath);
        // 2.生成对应作业
        createWork(filePath);
    }
    /**
     * 上传附件文件
     * @param file 前端传入的zip
     * @param filePath 目标路径
     * @return 存放文件的名称
     */
    private void uploadAttachment(MultipartFile file, FilePath filePath){

        // 创建文件对象
        File dest = new File(FILE_UPLOAD_PATH + filePath.toString() + File.separator + "publish" + File.separator + filePath.getSeq() + ".zip");
        // 检测是否存在目录
        if(!dest.getParentFile().exists()){
            // 新建文件夹
            dest.getParentFile().mkdirs();
        }
        try {
            // 文件写入
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    /**
     * 发布作业的同时生成学生作业
     * @param filePath
     */
    private void createWork(FilePath filePath){

        List<Student> studentList = studentMapper.selectStuByGradeid(filePath.getGradeid());
//        List<Student> studentList = studentRepository.findStuByGradeid(filePath.getGradeid());

        for(Student student : studentList){
            // 创建文件对象
            File dest = new File(FILE_UPLOAD_PATH
                    + filePath.toString() + student.getStudentid() + "_" + student.getUsername()  + "_" + filePath.getSeq() + ".doc");
            // 检测是否存在目录
            if(!dest.getParentFile().exists()){
                // 新建文件夹
                dest.getParentFile().mkdirs();
            }
            try {
                // 文件写入
                boolean newFile = dest.createNewFile();
                if(newFile){
                    logger.info("文件{}创建成功" , dest.getName());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
